<?php
namespace Home\Controller;
use Home\Controller\HomeController;
use Common\Api\SendPhoneCodeApi;
use Common\Model\MemberModel;
use Common\Model\UserRelationModel;
class RegController extends HomeController {
	//空操作
	public function _initialize(){
		parent::_initialize();
	}
	public function register(){
		$code=I('code');
		$this->assign('code',$code);
	    $this->display();
	}
	//根据邀请码查询用户是否存在
	public function getUserByInvitationCode($unique_code){
	    $where['unique_code'] = $unique_code;
	    return M('Invitation_code')->where($where)->find();
	}
	//注册
	public function reg(){
// 		$username=I('username')?I('username'):null;
		$phone = I('phone')?I('phone'):null;
		$phone_code = I('phone_code')?I('phone_code'):null;
		$pwd = I('password')?I('password'):null;
		$repwd=I('repwd')?I('repwd'):null;
		$pwdtrade = I('pwdtrade')?I('pwdtrade'):null;
		$retradepwd=I('retradepwd')?I('retradepwd'):null;
		$tuijian_name = I('tuijian_code');
		$USERMODEL = new MemberModel();
		if(empty($phone)){
	        $data_return['status'] = -5;
	        $data_return['info'] = '请输入手机号';
	        return $this->ajaxReturn($data_return);
	    }
// 	    if(empty($username)){
// 	    	$data_return['status'] = -5;
// 	    	$data_return['info'] = '请输入用户名';
// 	    	return $this->ajaxReturn($data_return);
// 	    }
	    if(!regex($phone,'phone')){
	        $data_return['status'] = -8;
	        $data_return['info']   = '手机号格式错误';
	        return $this->ajaxReturn($data_return);
	    }
	    if(empty($phone_code)){
	        $data_return['status'] = -6;
	        $data_return['info'] = '请输入手机验证码';
	        return $this->ajaxReturn($data_return);
	    }
	    if(empty($pwd)){
	    	$data_return['status'] = -7;
	    	$data_return['info'] = '请输入密码';
	    	return $this->ajaxReturn($data_return);
	    }
	    if(empty($pwdtrade)){
	        $data_return['status'] = -7;
	        $data_return['info'] = '请输入交易密码';
	        return $this->ajaxReturn($data_return);
	    }
	    if(empty($repwd)){
	    	$data_return['status'] = -7;
	    	$data_return['info'] = '请重复密码';
	    	return $this->ajaxReturn($data_return);
	    }
	    if(empty($retradepwd)){
	    	$data_return['status'] = -7;
	    	$data_return['info'] = '请重复交易密码';
	    	return $this->ajaxReturn($data_return);
	    }
	    if($pwd!=$repwd){
	    	$data_return['status'] = -7;
	    	$data_return['info'] = '两次密码输入不同';
	    	return $this->ajaxReturn($data_return);
	    }
	    if($pwdtrade!=$retradepwd){
	    	$data_return['status'] = -7;
	    	$data_return['info'] = '两次交易密码输入不同';
	    	return $this->ajaxReturn($data_return);
	    }
	    if(empty($tuijian_name)){
	        $data_return['status'] = -1;
	        $data_return['info'] = '请输入推荐人';
	        return $this->ajaxReturn($data_return);
	    }
	     if(!regex($pwd,'password')){
		     $data_return['status'] = -9;
		     $data_return['info'] = '请输入6~20位数字字母组成的密码';
		     return $this->ajaxReturn($data_return);
	     } 
	     if(!regex($pwdtrade,'password')){
	     	$data_return['status'] = -9;
	     	$data_return['info'] = '请输入6~20位数字字母组成的交易密码';
	     	return $this->ajaxReturn($data_return);
	     }
// 	     $check_username = $USERMODEL->getUserByUsername($username);
// 	     if($check_username){
// 	     	$data_return['status'] = -11;
// 	     	$data_return['info'] = '用户名已存在';
// 	     	return $this->ajaxReturn($data_return);
// 	     }
	    $check_phone = $USERMODEL->getUserByPhone($phone);
	    if($check_phone){
	        $data_return['status'] = -11;
	        $data_return['info'] = '手机号已存在';
	        return $this->ajaxReturn($data_return);
	    }
	    $tuijian_user = $this->getUserByInvitationCode($tuijian_name);
	    if(!$tuijian_user){
	        $data_return['status'] = -3;
	        $data_return['info']   = '推荐人不存在';
	        return $this->ajaxReturn($data_return);
	    }
	  	if($phone_code != session('codezhuce'.$phone)){
	        $data_return['status'] = -13;
	        $data_return['info']   = '手机验证码有误';
	        return $this->ajaxReturn($data_return);
	    }
		//开启事物
		M()->startTrans();
		//注册
		$data_user['username'] = $phone;
		$data_user['phone'] = $phone;
		$data_user['reg_time'] = time();
		$data_user['pid'] = $tuijian_user['uid'];
		$data_user['pwd'] = md5($pwd);
		$data_user['pwdtrade'] = md5($pwdtrade);
		$relation= $this->getRelation();
		$data_user['relation'] =$tuijian_user['relation'].$relation;
		$uid = M('Member')->add($data_user);
		$data_user_relation['uid'] = $uid;
		$data_user_relation['pid'] = $tuijian_user['uid']?$tuijian_user['uid']:'';
		$r[] = M('User_relation')->add($data_user_relation);
		//生成钱包地址
		do{
			$tag=rand(10000, 99999);
			$check=M('wallet')->where(["eos_tag"=>$tag])->find();
		}while($check);
		$data_user_address['eos_tag'] = $tag;
		$data_user_address['uid'] = $uid;
		$data_user_address['miyao'] = $this->getUrl();
		$r[] = M('Wallet')->add($data_user_address);
		$data_profit['uid']=$uid;
		$r[]=M('profit')->add($data_profit);
		//用户资金表
		$currencylist=M('Currency')->select();
		foreach ($currencylist as $k=>$v){
			if($v['currency_id']==5&&$this->config['reg_num']!=0){
				$data_money1['num']          = $this->config['reg_num'];
				$arr['uid']=$uid;
				$arr['add_time']=time();
				$arr['num']=$this->config['reg_num'];
				$arr['content']="新用户奖励";
				M('sweet_log')->add($arr);
				$this->addFinance($uid, 1, '新用户奖励', $this->config['reg_num'], 1,5);
			}
			$data_money1['uid']          = $uid;
			$data_money1['currency_id']          = $v['currency_id'];
			$r[] = M('Member_money')->add($data_money1);
		}
		//邀请码
		$this->getInvitationCode($uid);
		if (in_array(false, $r)){
		    M()->rollback();
		    $msg['status']=-7;
		    $msg['info']='系统繁忙';
		    $this->ajaxReturn($msg);
		}else {
		    M()->commit();
		    $msg['status']=1;
		    $msg['info']='注册成功';
		    $this->ajaxReturn($msg);
		}
	}
	//生成唯一编码 并添加数据库
	public function getInvitationCode($uid){
	    $unique_code = getRandom(8);
	    $code = $this->getUserByInvitationCode($unique_code);//根据生成的唯一编码  查询表中是否存在
	    if($code){
	        $this->getInvitationCode();
	    }
	    $data['uid'] = $uid;
	    $data['unique_code'] = $unique_code;
	    return M('Invitation_code')->add($data);
	}
	public function getRelation(){
		for($i=0;$i<3;$i++) {
			$data = 'abcdefghijkmnpqrstuvwxy';
			$fontcontent = substr($data, rand(0, strlen($data)-1), 1);
			$captch_code .= $fontcontent;
		}
		return $captch_code;
	}
	//发送验证码
	public function sendPhone(){
		$phone = I('phone')?I('phone'):null;
		$code_name = 'zhuce';
		if(empty($phone)){
			$data_return['status'] = -41;
			$data_return['info']   = '请输入手机号';
			return $this->ajaxReturn($data_return);
		}
		if(!regex($phone,'phone')){
			$data_return['status'] = -8;
			$data_return['info']   = '手机号格式错误';
			return $this->ajaxReturn($data_return);
		}
		$USER = new MemberModel();
		$checkphone = $USER->getUserByPhone($phone);
		//注册的时候检测手机号是否注册
		if($checkphone){
			$data_return['status'] = -42;
			$data_return['info']   = '手机号已存在';
			return $this->ajaxReturn($data_return);
		}		
		$SENDPHONE = new SendPhoneCodeApi($phone);
		$send = $SENDPHONE->send($code_name);
		if($send == 1){
			$data_return['status'] = 1;
			$data_return['info']   = '发送成功';
			return $this->ajaxReturn($data_return);
		}else{
			$data_return['status'] = -1;
			$data_return['info']   = $send;
			return $this->ajaxReturn($data_return);
		}
	}
	/**
	 * 增加验证码
	 */
	public function getVerify(){
		header('Access-Control-Allow-Origin: *');
		$config =    array(
				'fontSize'    =>    12,    // 验证码字体大小
				'length'      =>    4,     // 验证码位数
				'useNoise'    =>    false, // 关闭验证码杂点
				'useCurve'    =>	false,
				//'useImgBg'	=>true,
		);
		$Verify = new \Think\Verify($config);
		$Verify->entry();
	}
	public function code(){
	    $this->display();
	}
	public function register1(){
	    $this->display();
	}
	public function aaa(){
		$arr=array();
		$arr[1]=array("num"=>0);
		$arr[2]=array("num"=>1);
		dump($arr);
		foreach($arr as $k=>&$v){
			$arr[2]['num']=2;
			if($k==2){
				dump($v['num']);
			}
		}
		dump($arr);
	}
}